using System;
using System.IO;
using pixolut.BizBlox;

namespace BizBloxCSExamples
{
	/// <summary>
	/// Summary description for Setup.
	/// </summary>
	public class Setup
	{
		private static Random number = new Random();

		public Setup()
		{

		}

		public static void CreateDataSet()
		{	
			Console.WriteLine("Enter Setup.CreateDataSet() [please wait...]");
			Console.Out.Flush();
			DataAccess dObj = new DataAccess();
			Guid[] foodUid = new Guid[50]; 
			for(int i = 0; i < foodUid.Length; i++)
			{
				foodUid[i] = Guid.NewGuid();
				 dObj.RunSQL("INSERT INTO [Food](FoodID, Name, Price)VALUES('" + foodUid[i].ToString() + "','" + ComposeFoodName(i) + "','" + number.Next(6).ToString() + "')"); 
			}

			for(int i = 0; i < 100; i++)
			{
				Guid studentId = Guid.NewGuid();
				string studentName = ComposeStudentName(i);
				dObj.RunSQL("INSERT INTO [Student](StudentID, Name, Email, Age)VALUES('" + studentId.ToString() + "','" + studentName + "','" + ComposeStudentEmail(studentName) + "', '" + (number.Next(10) + 10).ToString() + "')"); 
				dObj.RunSQL("INSERT INTO [LunchOrder](LunchOrderID, StudentID, FoodID, dt_OrderDate, Quantity) VALUES('" + Guid.NewGuid().ToString() + "','" + studentId.ToString() + "', '" + foodUid[number.Next(foodUid.Length - 1)].ToString() + "', '" + DateTime.Now.AddDays(number.Next(5)).ToString("u") + "', '" + number.Next(1, 4).ToString() + "')"); 
			}
			Console.WriteLine("Exit Setup.CreateDataSet()");
			Console.Out.Flush();
		}

		public static void ClearDataSet()
		{
			Console.WriteLine("Enter Setup.ClearDataSet()");
			DataAccess dObj = new DataAccess();
			dObj.RunSQL("DELETE FROM [Food]");
			dObj.RunSQL("DELETE FROM [Student]");
			dObj.RunSQL("DELETE FROM [LunchOrder]");
			Console.WriteLine("Exit Setup.ClearDataSet()");
		}

		private static string ComposeFoodName(int i)
		{
			string[] foodSuffix = {"pie", "roll", "souffle", "pasta", "sandwich"}; 
			return Dictionary.RandomEntry() + " " + foodSuffix[number.Next(foodSuffix.Length - 1)];
		}

		private static string ComposeStudentName(int i)
		{
			return Dictionary.Entry(i) + " " + Dictionary.RandomEntry();
		}

		private static string ComposeStudentEmail(string name)
		{
			string prefix = name.Replace(" ", "_");
			string[] webSuffix = {"com", "edu", "gov", "mil", "org"};
			return prefix + "@" + Dictionary.RandomEntry() + "." + webSuffix[number.Next(webSuffix.Length -1)];
		}
	}
}
